package com.kkb.robotcustomerservice.controller;


import com.kkb.robotcustomerservice.config.BM25SimilarityConfig;
import com.kkb.robotcustomerservice.service.GeneralAnswerService;
import com.kkb.robotcustomerservice.service.GeneralProblemsService;
import com.kkb.robotcustomerservice.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@CrossOrigin
public class FAQController {
    @Autowired
    GeneralProblemsService generalProblemsService;
    @Autowired
    GeneralAnswerService generalAnswerService;

    @Autowired
    BM25SimilarityConfig config;

    //基于 FAQ 的问答接口
    @RequestMapping(value = "/qa_faq",method = RequestMethod.POST)
    public ResponseResult getAnswer(@RequestBody RequestData<FAQInput> requestData){

        FAQInput faqInput = requestData.getData();
        String words = faqInput.getQuery();

        //传入string得到置信度和问题ID
        Map map = config.getConfidence(words);

        double confidence = (double) map.get("confidence");
        Integer qid = (Integer) map.get("qid");

        //查询答案
        String answer = generalAnswerService.getByQid(qid).getContent();

        //返回数据
        FAQOutput faqOutput = new FAQOutput();
        //回答
        faqOutput.setAnswer(answer);
        //置信度
        faqOutput.setConfidence(confidence);
        //label 为系统对答案是否足够可靠的判断。1 表示系统认为答案可靠；0 表示系统认为答案不可靠
        if (faqOutput.getConfidence()>0.68){
            faqOutput.setLabel(1);
        }else {
            faqOutput.setLabel(0);
        }
        return ResponseResult.ok("success",faqOutput);
    }

}
